<?php
/**
* 名称：文件上传演示类。
* 描述：演示基于tryphp.library.uploader程式的上传文件的方式。
*/
final class Controller_Upload extends Controller{
    /**
    * 描述：默认执行程式。
    * @access protected
    * @return void
    */
    protected function Main(){
        self::$Template->Display('upload.html');
    }

    /**
    * 描述：单文件上传，要求必须上传的程式。
    * @access protected
    * @return void
    */
    protected function SingleMust(){
        $ups=new Uploader('file1');
        $ups->Type=Uploader::UPLOADTYPE_SINGLE;
        $ups->Size=1024*200;
        $ups->Extension=explode(',',Uploader::UPLOAD_EXT_IMAGE);
        $ups->BasePath=BUILD_ROOT;
        $ups->LocatePath='/data/000/unclassified';
        $ups->Execute();
        switch($ups->ErrorCode){
            case Uploader::UPLOAD_ERR_OK:
                echo sprintf('上传成功，文件名是%s。',$ups->Filename);
            break;
            case Uploader::UPLOAD_ERR_NO_FILE:
                echo '请上传文件。';
            break;
            case Uploader::UPLOAD_ERR_CUSTOM_EXTENSION:
                echo '扩展名不正确。';
            break;
            case Uploader::UPLOAD_ERR_CUSTOM_SIZE:
                echo '文件大小不正确。';
            break;
            case Uploader::UPLOAD_ERR_CUSTOM_NOFIELD:
                echo '发生异常，请稍候再试！~。';
            break;
            default:
                echo '上传失败，请稍候再试！';
            break;
        }
        //--
        echo  '<hr/>',$ups->PrintDebugInfo();
    }

    /**
    * 描述：单文件上传，可不上传文件的程式。
    * @access protected
    * @return void
    */
    protected function SingleChoice(){
        $ups=new Uploader('file1');
        $ups->Type=Uploader::UPLOADTYPE_SINGLE;
        $ups->Size=204800;
        $ups->Extension=explode(',',Uploader::UPLOAD_EXT_IMAGE);
        $ups->BasePath=BUILD_ROOT;
        $ups->LocatePath='/data/000/unclassified';
        $ups->Execute();
        if($ups->ErrorCode!==Uploader::UPLOAD_ERR_NO_FILE){
            switch($ups->ErrorCode){
                case Uploader::UPLOAD_ERR_OK:
                    echo '上传成功，文件名是：'.$ups->Filename.'。';
                break;
                case Uploader::UPLOAD_ERR_CUSTOM_EXTENSION:
                    echo '扩展名不正确。';
                break;
                case Uploader::UPLOAD_ERR_CUSTOM_SIZE:
                    echo '文件大小不正确。';
                break;
                case Uploader::UPLOAD_ERR_CUSTOM_NOFIELD:
                    echo '发生异常，请稍候再试！~。';
                break;
                default:
                    echo '文件上传失败，请稍候再试！~';
                break;
            }
        }
        //--
        echo  '<hr/>',$ups->PrintDebugInfo();
    }

    /**
    * 描述：一元多文件上传，要求全部合格才能上传。
    * @access protected
    * @return void
    */
    protected function MultiAll(){
        $ups=new Uploader('file1','file2|文件b');
        $ups->AddField('file3');
        $ups->Type=Uploader::UPLOADTYPE_MULTI_ALL;
        $ups->Extension=explode(',',Uploader::UPLOAD_EXT_IMAGE);
        $ups->Size=204800;
        $ups->BasePath=BUILD_ROOT;
        $ups->LocatePath='/data/000/unclassified';
        $ups->Execute();
        if($ups->ErrorCode['default']){
            echo '上传失败，请稍候再试!~';
        }
        else{
            if($ups->UploadCount===0){
                foreach($ups->ErrorCode['field'] as $fieldName=>$errorCode){
                    $fieldSituatedName=$ups->Field[$fieldName]['situatedName'];
                    //--
                    switch($errorCode){
                        case Uploader::UPLOAD_ERR_OK:
                            continue;
                        break;
                        case Uploader::UPLOAD_ERR_NO_FILE:
                            echo sprintf('%s：请上传文件。<br/>',$fieldSituatedName);
                        break;
                        case Uploader::UPLOAD_ERR_CUSTOM_EXTENSION:
                            echo sprintf('%s：扩展名不正确，扩展名只能是%s。<br/>',$fieldSituatedName,join(',',$ups->Field[$fieldName]['extension']));
                        break;
                        case Uploader::UPLOAD_ERR_CUSTOM_SIZE:
                            echo sprintf('%s：文件大小不正确。<br/>',$fieldSituatedName);
                        break;
                        case Uploader::UPLOAD_ERR_CUSTOM_NOFIELD:
                            echo sprintf('%s：发生异常，请稍候再试！~<br/>',$fieldSituatedName,$fieldName);;
                        break;
                        default:
                            echo sprintf('%s：文件上传失败，请稍候再试！~',$fieldSituatedName);
                        break;
                    }
                }
            }
            else{
                foreach($ups->Filename as $fieldName=>$fileName){
                    echo sprintf('%s:%s<br/>',$ups->Field[$fieldName]['situatedName'],$fileName);
                }
            }
        }
        //--
        echo  '<hr/>',$ups->PrintDebugInfo();
    }

    /**
    * 描述：单元多文件上传，只上传合格的文件。
    * @access protected
    * @return void
    */
    protected function MultiPart(){
        $ups=new Uploader('file1','file2|文件2');
        $ups->AddField('file3');
        $ups->Type=Uploader::UPLOADTYPE_MULTI_PART;
        $ups->Size=204800;
        $ups->Extension=explode(',',Uploader::UPLOAD_EXT_IMAGE);
        $ups->BasePath=BUILD_ROOT;
        $ups->LocatePath='/data/000/unclassified';
        $ups->Execute();
        if($ups->ErrorCode['default']){
            echo '上传失败，请稍候再试!~';
        }
        else{
            if($ups->UploadCount===0){
                XXX:
                $failCount=-1;
                foreach($ups->ErrorCode['field'] as $fieldName=>$errorCode){
                    $fieldSituatedName=$ups->Field[$fieldName]['situatedName'];
                    //--
                    switch($errorCode){
                        case Uploader::UPLOAD_ERR_OK:
                        case Uploader::UPLOAD_ERR_NO_FILE:
                            continue;
                        break;
                        case Uploader::UPLOAD_ERR_CUSTOM_EXTENSION:
                            $failCount++;
                            echo sprintf('%s：扩展名不正确，扩展名只能是%s。<br/>',$fieldSituatedName,join(',',$ups->Field[$fieldName]['extension']));
                        break;
                        case Uploader::UPLOAD_ERR_CUSTOM_SIZE:
                            $failCount++;
                            echo sprintf('%s：文件大小不正确。<br/>',$fieldSituatedName);
                        break;
                        case Uploader::UPLOAD_ERR_CUSTOM_NOFIELD:
                            $failCount++;
                            echo sprintf('%s：发生异常，请稍候再试！~<br/>',$fieldSituatedName,$fieldName);;
                        break;
                        default:
                            $failCount++;
                            echo sprintf('%s：文件上传失败！~<br/>',$fieldSituatedName);
                        break;
                    }
                }
                //--
                echo sprintf('上传成功%s个。<br/>',$ups->UploadCount);
                if($failCount!==-1){
                    echo sprintf('上传失败%s个。<br/>',++$failCount);
                }
            }
            else{
                //处理正确的。
                foreach($ups->Filename as $fieldName=>$fileName){
                    echo sprintf('%s:%s<br/>',$ups->Field[$fieldName]['situatedName'],$fileName);
                }
                //报告错误的。
                if($ups->UploadCount!==$ups->FieldCount){
                    goto XXX;
                }
            }
        }
        //--
        echo  '<hr/>',$ups->PrintDebugInfo();
    }

    /**
    * 描述：多元多文件上传，要求全部合格才能上传。
    * @access protected
    * @return void
    */
    protected function MultiplexAll(){
        $ups=new Uploader();
        $ups->Type=Uploader::UPLOADTYPE_MULTIPLEX_ALL;
        $ups->AddField('file1|文件1',102400,explode(',',Uploader::UPLOAD_EXT_IMAGE),BUILD_ROOT,'/data/000/unclassified/field11');
        $ups->AddField('file2|文件2',307200,explode(',',Uploader::UPLOAD_EXT_IMAGE),BUILD_ROOT,'/data/000/unclassified/field22');
        $ups->AddField('file3|文件3',204800,explode(',',Uploader::UPLOAD_EXT_IMAGE),BUILD_ROOT,'/data/000/unclassified/field33');
        $ups->Execute();
        if($ups->UploadCount===0){
            foreach($ups->ErrorCode as $fieldName=>$errorCode){
                $fieldSituatedName=$ups->Field[$fieldName]['situatedName'];
                switch($errorCode){
                    case Uploader::UPLOAD_ERR_OK:
                        continue;
                    break;
                    case Uploader::UPLOAD_ERR_NO_FILE:
                        echo sprintf('%s：请上传文件。<br/>',$fieldSituatedName);
                    break;
                    case Uploader::UPLOAD_ERR_CUSTOM_EXTENSION:
                        echo sprintf('%s：扩展名不正确，扩展名只能是%s。<br/>',$fieldSituatedName,join(',',$ups->Field[$fieldName]['extension']));
                    break;
                    case Uploader::UPLOAD_ERR_CUSTOM_SIZE:
                        echo sprintf('%s：文件大小不正确。<br/>',$fieldSituatedName);
                    break;
                    case Uploader::UPLOAD_ERR_CUSTOM_NOFIELD:
                        echo sprintf('%s：发生异常，请稍候再试！~<br/>',$fieldSituatedName,$fieldName);;
                    break;
                    default:
                        echo sprintf('%s：文件上传失败，请稍候再试！~<br/>',$fieldSituatedName);
                    break;
                }
            }
        }
        else{
            foreach($ups->Filename as $fieldName=>$fileName){
                echo sprintf('%s:%s<br/>',$ups->Field[$fieldName]['situatedName'],$fileName);
            }
        }
        //--
        echo  '<hr/>',$ups->PrintDebugInfo();
    }

    /**
    * 描述：多元多文件上传，部分合格就可以上传。
    * @access protected
    * @return void
    */
    protected function MultiplexPart(){
        $ups=new Uploader();
        $ups->Type=Uploader::UPLOADTYPE_MULTIPLEX_PART;
        $ups->AddField('file1|文件1',102400,explode(',',Uploader::UPLOAD_EXT_IMAGE),BUILD_ROOT,'/data/000/unclassified/field11');
        $ups->AddField('file2|文件2',307200,explode(',',Uploader::UPLOAD_EXT_IMAGE),BUILD_ROOT,'/data/000/unclassified/field22');
        $ups->AddField('file3|文件3',204800,array('doc','docx','txt'),BUILD_ROOT,'/data/000/unclassified/field33');
        $ups->Execute();
        if($ups->UploadCount===0){
            YYY:$failCount=-1;
            foreach($ups->ErrorCode as $fieldName=>$errorCode){
                $fieldSituatedName=$ups->Field[$fieldName]['situatedName'];
                //--
                switch($errorCode){
                    case Uploader::UPLOAD_ERR_OK:
                    case Uploader::UPLOAD_ERR_NO_FILE:
                        continue;
                    break;
                    case Uploader::UPLOAD_ERR_CUSTOM_EXTENSION:
                        $failCount++;
                        echo sprintf('%s：扩展名不正确，扩展名只能是%s。<br/>',$fieldSituatedName,join(',',$ups->Field[$fieldName]['extension']));
                    break;
                    case Uploader::UPLOAD_ERR_CUSTOM_SIZE:
                        $failCount++;
                        echo sprintf('%s：文件大小不正确。<br/>',$fieldSituatedName);
                    break;
                    case Uploader::UPLOAD_ERR_CUSTOM_NOFIELD:
                        $failCount++;
                        echo sprintf('%s：发生异常，请稍候再试！~<br/>',$fieldSituatedName,$fieldName);;
                    break;
                    default:
                        $failCount++;
                        echo sprintf('%s：文件上传失败！~<br/>',$fieldSituatedName);
                    break;
                }
            }
            //--
            echo sprintf('上传成功%s个。<br/>',$ups->UploadCount);
            if($failCount!==-1){
                echo sprintf('上传失败%s个。<br/>',++$failCount);
            }
        }
        else{
            //处理正确的。
            foreach($ups->Filename as $fieldName=>$fileName){
                echo sprintf('%s:%s<br/>',$ups->Field[$fieldName]['situatedName'],$fileName);
            }
            //报告错误的。
            if($ups->UploadCount!==$ups->FieldCount){
                goto YYY;
            }
        }
        //--
        echo  '<hr/>',$ups->PrintDebugInfo();
    }
}